<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
    <BasicForm @register="registerForm" name="BizChargeStakeForm">
      <template #operatorIdSlot2="{ model, field }">
        <a-select ref="select" v-model:value="model[field]">
          <a-select-option v-for="item in operatorIdList" :value="item.value">{{ item.label
            }}</a-select-option>
        </a-select>
      </template>

      <template #stationIdSlot2="{ model, field }">
        <a-select ref="select" v-model:value="model[field]">
          <a-select-option v-for="item in stationIdList" :value="item.value">{{ item.label
            }}</a-select-option>
        </a-select>
      </template>
    </BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
import {ref, computed, unref, onMounted} from 'vue';
import {BasicModal, useModalInner} from '/@/components/Modal';
import {BasicForm, useForm} from '/@/components/Form/index';
import {formSchema} from '../BizChargeStake.data';
import {saveOrUpdate} from '../BizChargeStake.api';
// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const isDetail = ref(false);
//表单配置
const [registerForm, {getFieldsValue, setProps, resetFields, setFieldsValue, validate, scrollToField, removeSchemaByFiled}] = useForm({
	//labelWidth: 150,
	schemas: formSchema,
	showActionButtonGroup: false,
	baseColProps: {span: 24},
});
//表单赋值
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
	//重置表单
	await resetFields();
	setModalProps({confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter});
	isUpdate.value = !!data?.isUpdate;
	isDetail.value = !!data?.showFooter;
	if (unref(isUpdate)) {
		//表单赋值
		await setFieldsValue({
			...data.record,
		});
	}
	// 不同状态隐藏条件
	if (isDetail.value) {
		removeSchemaByFiled(['dcChargers', 'acChargers', 'totalPower']);
	}
	// 隐藏底部时禁用整个表单
	setProps({disabled: !data?.showFooter});
});

// 所属运营商下拉框数据
const operatorIdList = ref([]);
const stationIdList = ref([]);
onMounted(async () => {
	//获取下拉框数据
	operatorIdList.value = JSON.parse(window.localStorage.getItem('operatorIdList'));
	stationIdList.value = JSON.parse(window.localStorage.getItem('stationIdList'));
});

//设置标题
const title = computed(() => (!unref(isUpdate) ? '新增' : !unref(isDetail) ? '详情' : '编辑'));
//表单提交事件
async function handleSubmit(v) {
	try {
		let values = await validate();
		setModalProps({confirmLoading: true});
		//提交表单
		await saveOrUpdate(values, isUpdate.value);
		//关闭弹窗
		closeModal();
		//刷新列表
		emit('success');
	} catch ({errorFields}) {
		if (errorFields) {
			const firstField = errorFields[0];
			if (firstField) {
				scrollToField(firstField.name, {behavior: 'smooth', block: 'center'});
			}
		}
		return Promise.reject(errorFields);
	} finally {
		setModalProps({confirmLoading: false});
	}
}
</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
	width: 100%;
}

:deep(.ant-calendar-picker) {
	width: 100%;
}
</style>
